package com.dankegongyu.lib.common.util.statusbar;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.ColorRes;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

/**
 * 状态栏等工具类
 *
 * @author wupuquan
 * @version 1.0
 * @since 2018/7/24 15:37
 */
public class DKStatusBarUtils {

    /**
     * 获取状态栏高度
     *
     * @return 状态栏高度
     */
    public static int getStatusBarHeight() {
        // 获得状态栏高度
        Resources resources = Resources.getSystem();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        return resources.getDimensionPixelSize(resourceId);
    }

    /**
     * 获取ActionBar高度
     *
     * @param activity activity
     * @return ActionBar高度
     */
    public static int getActionBarHeight(@NonNull final Activity activity) {
        TypedValue tv = new TypedValue();
        if (activity.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            return TypedValue.complexToDimensionPixelSize(tv.data, activity.getResources().getDisplayMetrics());
        }
        return 0;
    }

    /**
     * 设置状态栏颜色
     *
     * @param activity
     * @param color          颜色
     * @param statusBarAlpha 透明度
     */
    public static void setColor(Activity activity, @ColorRes int color, @IntRange(from = 0, to = 255) int statusBarAlpha) {
        if (activity != null) {
            int colorInt = ContextCompat.getColor(activity, color);
            StatusBarUtil.setColor(activity, colorInt, statusBarAlpha);
        }
    }

    /**
     * 设置状态栏全透明
     *
     * @param activity 需要设置的activity
     */
    public static void setTransparent(Activity activity) {
        if (activity != null) {
            StatusBarUtil.setTransparent(activity);
        }
    }

    /**
     * 使状态栏半透明
     * <p>
     * 适用于图片作为背景的界面,此时需要图片填充到状态栏
     *
     * @param activity       需要设置的activity
     * @param statusBarAlpha 状态栏透明度
     */
    public static void setTranslucent(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) {
        if (activity != null) {
            StatusBarUtil.setTranslucent(activity, statusBarAlpha);
        }
    }

    /**
     * 亮色模式，如：白底黑字
     *
     * @param activity target Activity
     */
    public static void setLightMode(Activity activity) {
        if (activity != null) {
            StatusBarUtil.setLightMode(activity);
        }
    }

    /**
     * 暗色模式，如：黑底白字
     *
     * @param activity target Activity
     */
    public static void setDarkMode(Activity activity) {
        if (activity != null) {
            StatusBarUtil.setDarkMode(activity);
        }
    }

    /**
     * 亮色模式 && 状态栏透明
     */
    public static void setTransparentLightMode(Activity activity) {
        if (activity != null) {
            StatusBarUtil.setLightMode(activity);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            }
        }
    }

    /**
     * 暗色模式 && 状态栏透明
     */
    public static void setTransparentDarkMode(Activity activity) {
        if (activity != null) {
            StatusBarUtil.setDarkMode(activity);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            }
        }
    }

    public static void setPlaceholderForTranslucent(Context context, View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int statusBarHeight = DKStatusBarUtils.getStatusBarHeight();
            view.setPadding(0, statusBarHeight, 0, 0);
        }
    }

    public static void setPlaceholderForColor(Context context, View view, @ColorRes int colorRes) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int color = ContextCompat.getColor(context, colorRes);
            int statusBarHeight = DKStatusBarUtils.getStatusBarHeight();
            view.setPadding(0, statusBarHeight, 0, 0);
            view.setBackgroundColor(color);
        }
    }

    /**
     * 适用于顶部是ImageView的界面 设置状态栏透明
     *
     * @param activity 需要设置的activity
     */
    public static void setTranslucentForImageViewLightMode(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha, View view) {
        if (activity != null) {
            StatusBarUtil.setTranslucentForImageView(activity, statusBarAlpha, view);
            setTransparentLightMode(activity);
        }
    }

    /**
     * 适用于顶部是ImageView的界面 设置状态栏透明
     *
     * @param activity 需要设置的activity
     */
    public static void setTranslucentForImageViewDarkMode(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha, View view) {
        if (activity != null) {
            StatusBarUtil.setTranslucentForImageView(activity, statusBarAlpha, view);
            setTransparentDarkMode(activity);
        }
    }

    /**
     * 为 fragment 头部是 ImageView 的设置状态栏透明
     *
     * @param activity       fragment 对应的 activity
     * @param statusBarAlpha 状态栏透明度
     * @param needOffsetView 需要向下偏移的 View
     */
    public static void setTranslucentForInFragment(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha,
                                                   View needOffsetView) {
        if (activity != null) {
            StatusBarUtil.setTranslucentForImageViewInFragment(activity, statusBarAlpha, needOffsetView);
        }
    }

    /**
     * 为滑动返回界面设置状态栏颜色
     *
     * @param activity       需要设置的activity
     * @param color          状态栏颜色值
     * @param statusBarAlpha 状态栏透明度
     */
    public static void setColorForSwipeBackLightMode(Activity activity, @ColorRes int color,
                                                     @IntRange(from = 0, to = 255) int statusBarAlpha) {
        if (activity != null) {
            int colorInt = ContextCompat.getColor(activity, color);
            StatusBarUtil.setColorForSwipeBack(activity, colorInt, statusBarAlpha);
            setTransparentLightMode(activity);
        }
    }

    /**
     * 为滑动返回界面设置状态栏颜色
     *
     * @param activity       需要设置的activity
     * @param color          状态栏颜色值
     * @param statusBarAlpha 状态栏透明度
     */
    public static void setColorForSwipeBackDarkMode(Activity activity, @ColorRes int color,
                                                    @IntRange(from = 0, to = 255) int statusBarAlpha) {
        if (activity != null) {
            int colorInt = ContextCompat.getColor(activity, color);
            StatusBarUtil.setColorForSwipeBack(activity, colorInt, statusBarAlpha);
            setTransparentDarkMode(activity);
        }
    }

    /**
     * 为view增加MarginTop为状态栏高度
     *
     * @param view view
     */
    public static void addMarginTopEqualStatusBarHeight(@NonNull Context context, View view) {
        int TAG_OFFSET = -123;
        Object haveSetOffset = view.getTag(TAG_OFFSET);
        if (haveSetOffset != null && (Boolean) haveSetOffset) return;
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(layoutParams.leftMargin,
                layoutParams.topMargin + getStatusBarHeight(),
                layoutParams.rightMargin,
                layoutParams.bottomMargin);
        view.setTag(TAG_OFFSET, true);
    }

    /**
     * 隐藏伪状态栏 View
     */
    public static void hideFakeStatusBarView(@NonNull Activity activity) {
        StatusBarUtil.hideFakeStatusBarView(activity);
    }
}
